package com.hnkywl.sts.action.organ;

import com.hnkywl.sts.entity.organ.Zyf;
import com.hnkywl.sts.service.organ.ZyfService;
import net.ruixin.web.struts2.SimpleActionSupport;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * Created by Melody on 14-6-24.
 */
@Namespace("/organ/zyf")
public class ZyfAction extends SimpleActionSupport {

    private ZyfService zyfService;

    //质押方全称
    private String qc;
    //质押方简称
    private String jc;
    //分页组件 起始位置
    private int page;
    //分页组件 页面大小
    private int rows;
    //远程ajax验证的值
    private String validateVal;
    //需要远程ajax验证的属性
    private String validateField;
    private Long zyfId;
    private Zyf zyf;

    @Action("zyfList")
    public void zyfList() {
        try {
            this.renderText(this.zyfService.findAllZyf(qc, jc, page, rows));
        } catch (Exception e) {
            this.renderJson("");
        }
    }

    @Action("zyf_content")
    public String zyfContent() {
        if (zyfId != null) {
            zyf = zyfService.findZyfDetail(zyfId);
        }
        return SUCCESS;
    }

    @Action("add_zyf")
    public void addzyf() {
        try {
            this.zyfService.saveZyf(zyf);
            renderText("{\"statusCode\":\"200\", \"message\":\"添加质押方成功！\", \"operationType\":\"add\"}");
        } catch (RuntimeException e) {
            e.printStackTrace();
            renderText("{\"statusCode\":\"300\", \"message\":\"" + e.getMessage() + "\"}");
        }
    }

    @Action("update_zyf")
    public void updateZyf() {
        try {
            this.zyfService.updateZyf(zyf);
            renderText("{\"statusCode\":\"200\", \"message\":\"修改质押方成功！\", \"operationType\":\"update\"}");
        } catch (RuntimeException e) {
            e.printStackTrace();
            renderText("{\"statusCode\":\"300\", \"message\":\"" + e.getMessage() + "\"}");
        }
    }

    @Action("delete_zyf")
    public void deleteZyf() {
        try {
            this.zyfService.deleteZyf(zyfId);
            renderText("{\"statusCode\":\"200\", \"message\":\"删除质押方成功！\", \"operationType\":\"delete\"}");
        } catch (Exception e) {
            e.printStackTrace();
            renderText("{\"statusCode\":\"300\", \"message\":\"" + e.getMessage() + "\"}");
        }
    }

    @Action("start_zyf")
    public void startZyf() {
        try {
            this.zyfService.startZyf(zyfId);
            renderJson("{\"statusCode\":\"200\", \"message\":\"启用成功\", \"operationType\":\"start\"}");
        } catch (Exception e) {
            e.printStackTrace();
            renderJson("{\"statusCode\":\"300\", \"message\":\"" + e.getMessage() + "\"}");
        }
    }

    @Action("stop_zyf")
    public void stopZyf() {
        try {
            this.zyfService.stopZyf(zyfId);
            renderJson("{\"statusCode\":\"200\", \"message\":\"停用成功\", \"operationType\":\"stop\"}");
        } catch (Exception e) {
            e.printStackTrace();
            renderJson("{\"statusCode\":\"300\", \"message\":\"" + e.getMessage() + "\"}");
        }
    }

    @Action("ajax_validate")
    public void validate1() {
        if (!zyfService.ajaxValidate(zyfId, validateVal, validateField))
            renderJson("{\"valid\":true}");
        else
            renderJson("{\"valid\":false}");
    }

    //查询所有的质押方简称
    @Action("zyfjc_list")
    public void zyfJcList() {
        this.renderJson(this.zyfService.findZyfJc(jc, 1, -1));
    }

    @Autowired
    public void setZyfService(ZyfService zyfService) {
        this.zyfService = zyfService;
    }

    public String getQc() {
        return qc;
    }

    public void setQc(String qc) {
        this.qc = qc;
    }

    public String getJc() {
        return jc;
    }

    public void setJc(String jc) {
        this.jc = jc;
    }

    public int getPage() {
        return page;
    }

    public void setPage(int page) {
        this.page = page;
    }

    public int getRows() {
        return rows;
    }

    public void setRows(int rows) {
        this.rows = rows;
    }

    public String getValidateVal() {
        return validateVal;
    }

    public void setValidateVal(String validateVal) {
        this.validateVal = validateVal;
    }

    public String getValidateField() {
        return validateField;
    }

    public void setValidateField(String validateField) {
        this.validateField = validateField;
    }

    public Long getZyfId() {
        return zyfId;
    }

    public void setZyfId(Long zyfId) {
        this.zyfId = zyfId;
    }

    public Zyf getZyf() {
        return zyf;
    }

    public void setZyf(Zyf zyf) {
        this.zyf = zyf;
    }
}
